package com.yanq.liancar.activity;
/*
 *  @创建者     liyan
 *  @创建时间   2018/11/17  10:32
 *  @描述       ${TODO}
 */


import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.gyf.barlibrary.ImmersionBar;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.vondear.rxtool.view.RxToast;
import com.yanq.liancar.R;
import com.yanq.liancar.activity.base.BaseToolbarActivity;
import com.yanq.liancar.adapter.DialogListAdapter;
import com.yanq.liancar.adapter.DialogListAdapter2;
import com.yanq.liancar.adapter.JiyouAdapter;
import com.yanq.liancar.adapter.PeijianAdapter;
import com.yanq.liancar.bean.BaseBean;
import com.yanq.liancar.bean.OBDPeiZhiListBean;
import com.yanq.liancar.bean.UserBean;
import com.yanq.liancar.url.Config;
import com.yanq.liancar.util.LogUtils;
import com.yanq.liancar.util.ToastUtils;
import com.yanq.liancar.view.SimplePaddingDecoration;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MaintainSetActivity extends BaseToolbarActivity {
    @BindView(R.id.recyclerview_jiyou)
    RecyclerView recyclerviewJiyou;
    @BindView(R.id.recyclerview_peijian)
    RecyclerView recyclerviewPeijian;
    private List<OBDPeiZhiListBean.DataBean._$1Bean> jyList = new ArrayList<>();
    private List<OBDPeiZhiListBean.DataBean._$2Bean> pjList = new ArrayList<>();

    private List<OBDPeiZhiListBean.DataBean._$1Bean.ObdPeiZhiDTOsBean> mIYList = new ArrayList<>();
    private List<OBDPeiZhiListBean.DataBean._$2Bean.ObdPeiZhiDTOsBeanX> mPJList = new ArrayList<>();
    private JiyouAdapter mJiyouAdapter;
    private PeijianAdapter mPeijianAdapter;
    private DialogListAdapter mDialogListAdapter;
    private DialogListAdapter2 mDialogListAdapter2;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ImmersionBar.with(this).statusBarColor(R.color.white).statusBarDarkFont(true).init();
        setContentView(R.layout.activity_maintainset);
        ImmersionBar.with(this).statusBarDarkFont(true).statusBarColor(R.color.white).init();
        ButterKnife.bind(this);
        mTvTitle.setText("养护设置");
        initRecyclerView();
        initOkGo();
    }

//    @OnClick({R.id.tv_buycartiime})
//    public void onViewClicked(View view) {
//        switch (view.getId()) {
//            case R.id.tv_buycartiime:                               //购买时间
//                yearPickerView();
//                break;
//        }
//    }

    private void initRecyclerView() {
        recyclerviewJiyou.setNestedScrollingEnabled(false);
        recyclerviewPeijian.setNestedScrollingEnabled(false);
        recyclerviewJiyou.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerviewPeijian.setLayoutManager(new LinearLayoutManager(mContext));
        mJiyouAdapter = new JiyouAdapter(R.layout.item_jiyou, jyList);
        mPeijianAdapter = new PeijianAdapter(R.layout.item_jiyou, pjList);
        recyclerviewJiyou.setAdapter(mJiyouAdapter);
        recyclerviewPeijian.setAdapter(mPeijianAdapter);

        mJiyouAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                switch (view.getId()) {
                    case R.id.tv_select:
                        mIYList = jyList.get(position).getObdPeiZhiDTOs();
                        myDialog(jyList.get(position).getId(),position,mIYList, (TextView) view);
                        break;
                    case R.id.tv_stutas2:
                        LogUtils.e("mIYList.size()======",jyList.get(position).getObdPeiZhiDTOs().size()+"");
                        if (jyList.get(position).getType()==1){
                            for (int i = 0; i <jyList.get(position).getObdPeiZhiDTOs().size() ; i++) {
                                if (jyList.get(position).getObdPeiZhiDTOs().get(i).getStatus()==1){
                                    maintain(jyList.get(position).getObdPeiZhiDTOs().get(i).getId(),jyList.get(position).getObdPeiZhiDTOs().get(i).getName());
                                }
                            }
                        }else {
                            maintain(jyList.get(position).getId(),jyList.get(position).getName());
                        }
                        jyList.get(position).setHealthStatus(1);
                        mJiyouAdapter.notifyDataSetChanged();

                        break;
                }
            }
        });

        mPeijianAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                switch (view.getId()) {
                    case R.id.tv_select:
                        mPJList = pjList.get(position).getObdPeiZhiDTOs();
                        myDialog1(pjList.get(position).getId(),position,mPJList , (TextView) view);
                        break;
                    case R.id.tv_stutas2:
                        if (pjList.get(position).getType()==1){
                            for (int i = 0; i <pjList.get(position).getObdPeiZhiDTOs().size(); i++) {
                                if (pjList.get(position).getObdPeiZhiDTOs().get(i).getStatus()==1){
                                    maintain(pjList.get(position).getObdPeiZhiDTOs().get(i).getId(),pjList.get(position).getObdPeiZhiDTOs().get(i).getName());
                                }
                            }
                        }else {
                            maintain(pjList.get(position).getId(),pjList.get(position).getName());
                        }
                        pjList.get(position).setHealthStatus(1);
                        mPeijianAdapter.notifyDataSetChanged();

                        break;
                }
            }
        });
    }

//    private void yearPickerView() {
//        Calendar selectedDate = Calendar.getInstance();
//        Calendar startDate = Calendar.getInstance();
//        Calendar endDate = Calendar.getInstance();
//
//        //正确设置方式 原因：注意事项有说明
//        startDate.set(1990, 0, 1);
//        endDate.set(2050, 11, 31);
//
//
//        TimePickerView pvTime = new TimePickerBuilder(this, new OnTimeSelectListener() {
//            @Override
//            public void onTimeSelect(Date date, View v) {
//
//                String Startdate = getTime(date);
//                tvBuycartiime.setText(Startdate);
//
//            }
//        })
//                .setType(new boolean[]{true, true, true, false, false, false})// 默认全部显示
//                .setCancelText("取消")//取消按钮文字
//                .setSubmitText("确定")//确认按钮文字
//                .setContentTextSize(18)//滚轮文字大小
//                .setTitleSize(20)//标题文字大小
//                .setTitleText("选择时间")//标题文字
//                .setOutSideCancelable(false)//点击屏幕，点在控件外部范围时，是否取消显示
//                .isCyclic(true)//是否循环滚动
//                .setTitleColor(Color.BLACK)//标题文字颜色
//                .setSubmitColor(getResources().getColor(R.color.main_color))//确定按钮文字颜色
//                .setCancelColor(getResources().getColor(R.color.main_color))//取消按钮文字颜色
//                .setTitleBgColor(0xFFd6d6d6)//标题背景颜色 Night mode
//                .setBgColor(0xFFffffff)//滚轮背景颜色 Night mode
//                .setDate(selectedDate)// 如果不设置的话，默认是系统时间*/
//                .setRangDate(startDate, endDate)//起始终止年月日设定
//                .setLabel("年", "月", "日", "时", "分", "秒")//默认设置为年月日时分秒
//                .isCenterLabel(false) //是否只显示中间选中项的label文字，false则每项item全部都带有label。
//                .isDialog(false)//是否显示为对话框样式
//                .setBgColor(0xFFffffff)
//                .build();
//        pvTime.show();
//    }

    private String getTime(Date date) {//可根据需要自行截取数据显示
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.format(date);
    }

    //机油弹窗
    private void myDialog(final int id, final int pos, final List<OBDPeiZhiListBean.DataBean._$1Bean.ObdPeiZhiDTOsBean> bean, final TextView textView) {
        final Dialog bottomDialog = new Dialog(this);
        View contentView = LayoutInflater.from(this).inflate(R.layout.dialog_list, null);
        bottomDialog.setContentView(contentView);
        ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) contentView.getLayoutParams();
        params.width = getResources().getDisplayMetrics().widthPixels - 100;
        contentView.setLayoutParams(params);
        bottomDialog.getWindow().setGravity(Gravity.BOTTOM);
        bottomDialog.getWindow().setWindowAnimations(R.style.BottomDialog_Animation);
        bottomDialog.setCanceledOnTouchOutside(true);
        bottomDialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        bottomDialog.show();

        TextView tv_cancle = bottomDialog.findViewById(R.id.tv_cancle);
        RecyclerView dialog_recyclerView = bottomDialog.findViewById(R.id.dialog_recyclerview);
        dialog_recyclerView.setNestedScrollingEnabled(false);
        dialog_recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        dialog_recyclerView.addItemDecoration(new SimplePaddingDecoration(this));
        mDialogListAdapter = new DialogListAdapter(R.layout.dialog_list_item, bean);
        dialog_recyclerView.setAdapter(mDialogListAdapter);

        mDialogListAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                textView.setText(bean.get(position).getName());
                bottomDialog.dismiss();
                setOBDpeizhi(id,jyList.get(pos).getObdPeiZhiDTOs().get(position).getId(),1,pos,position);
            }
        });

        tv_cancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                bottomDialog.dismiss();
            }
        });
    }


    //配件弹窗
    private void myDialog1(final int id, final int pos, final List<OBDPeiZhiListBean.DataBean._$2Bean.ObdPeiZhiDTOsBeanX> bean, final TextView textView) {
        final Dialog bottomDialog = new Dialog(this);
        View contentView = LayoutInflater.from(this).inflate(R.layout.dialog_list, null);
        bottomDialog.setContentView(contentView);
        ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) contentView.getLayoutParams();
        params.width = getResources().getDisplayMetrics().widthPixels - 100;
        contentView.setLayoutParams(params);
        bottomDialog.getWindow().setGravity(Gravity.BOTTOM);
        bottomDialog.getWindow().setWindowAnimations(R.style.BottomDialog_Animation);
        bottomDialog.setCanceledOnTouchOutside(true);
        bottomDialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        bottomDialog.show();

        TextView tv_cancle = bottomDialog.findViewById(R.id.tv_cancle);
        RecyclerView dialog_recyclerView = bottomDialog.findViewById(R.id.dialog_recyclerview);
        dialog_recyclerView.setNestedScrollingEnabled(false);
        dialog_recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        dialog_recyclerView.addItemDecoration(new SimplePaddingDecoration(this));
        mDialogListAdapter2 = new DialogListAdapter2(R.layout.dialog_list_item, bean);
        dialog_recyclerView.setAdapter(mDialogListAdapter2);

        mDialogListAdapter2.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                textView.setText(bean.get(position).getName());
                bottomDialog.dismiss();
                setOBDpeizhi(id,pjList.get(pos).getObdPeiZhiDTOs().get(position).getId(),2,pos,position);
            }
        });

        tv_cancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                bottomDialog.dismiss();
            }
        });
    }


    private void initOkGo() {
        OkGo.<String>get(Config.s + "llc/obdPeiZhiController/queryObdPeiZhiList")
                .tag(this)
                .params("token", UserBean.getcToken(this))
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        LogUtils.e("qeqweqe", response.body());
                        Gson gson = new Gson();
                        OBDPeiZhiListBean info = gson.fromJson(response.body(), OBDPeiZhiListBean.class);
                        if (info.isIsSuccess()) {
                            jyList.clear();
                            pjList.clear();
                            jyList.addAll(info.getData().get_$1());
                            pjList.addAll(info.getData().get_$2());
                            mJiyouAdapter.notifyDataSetChanged();
                            mPeijianAdapter.notifyDataSetChanged();
                        } else {
                            RxToast.error(info.getOutputMessage());
                        }
                    }
                });
    }

    //设置配置信息
    private void setOBDpeizhi(int id, int pid, final int type, final int pos, final int position){
        OkGo.<String>post(Config.s + "llc/obdPeiZhiSetController/addObdPeiZhiSet")
                .tag(this)
                .params("token", UserBean.getcToken(this))
                .params("pid", id)
                .params("id",pid)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        LogUtils.e("qeqweqe", response.body());
                        Gson gson = new Gson();
                        BaseBean info = gson.fromJson(response.body(), BaseBean.class);
                        if (info.isIsSuccess()) {
                            RxToast.success(info.getOutputMessage());
                            if (type==1){//配置机油
                                jyList.get(pos).getObdPeiZhiDTOs().get(position).setStatus(1);
                                jyList.get(pos).setStatus(1);
                                mJiyouAdapter.notifyDataSetChanged();
                            }else {
                                pjList.get(pos).getObdPeiZhiDTOs().get(position).setStatus(1);
                                pjList.get(pos).setStatus(1);
                                mPeijianAdapter.notifyDataSetChanged();
                            }

                        } else {
                            RxToast.error(info.getOutputMessage());
                        }
                    }
                });
    }

    //保养接口
    private void maintain(int id,String keepName){
        OkGo.<String>post(Config.s + "llc/obdKeepHisController/addObdKeepHisRecord")
                .tag(this)
                .params("token", UserBean.getcToken(this))
                .params("peiZhiId", id)
                .params("keepName",keepName)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        LogUtils.e("qeqweqe", response.body());
                        Gson gson = new Gson();
                        BaseBean info = gson.fromJson(response.body(), BaseBean.class);
                        if (info.isIsSuccess()) {
                            RxToast.success(info.getOutputMessage());
                        } else {
                            RxToast.error(info.getOutputMessage());
                        }
                    }
                });
    }

}
